package com.itboye.boyeshoping.base;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.itboye.boyeshoping.R;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by Administrator on 2018/6/26 0026.
 */

public class BaseActivity extends AppCompatActivity{
    private View statusBar;
//    private ViewContainer container;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//安卓布局 延伸到状态栏
                Window window = getWindow();
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                        | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

//                window.setFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
//                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);// 虚拟导航栏透明
                window.setFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                window.setStatusBarColor(getResources().getColor(R.color.maincolor));
                window.setNavigationBarColor(Color.TRANSPARENT);
            }

//            Window window = getWindow();
//            // Translucent status bar
//            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
//                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            statusBar = new View(this);
            ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight());
            statusBar.setLayoutParams(param);
            setStatusBarColor(R.color.maincolor);

//            container = new ViewContainer(getApplicationContext());
//            container.setOrientation(LinearLayout.VERTICAL);
//            container.addView(statusBar);
        }
        smartInject();
    }

    private void smartInject() {

        try {
            Class<? extends Activity> clz = getClass();

            while (clz != BaseActivity.class) {

                Field[] fs = clz.getDeclaredFields();
                Resources res = getResources();
                String packageName = getPackageName();
                for (Field field : fs) {
                    if (!View.class.isAssignableFrom(field.getType())) {
                        continue;
                    }
                    int viewId = res.getIdentifier(field.getName(), "id",
                            packageName);
                    if (viewId == 0)
                        continue;
                    field.setAccessible(true);
                    try {
                        View v = findViewById(viewId);
                        field.set(this, v);
                        Class<?> c = field.getType();
                        Method m = c.getMethod("setOnClickListener",
                                android.view.View.OnClickListener.class);
                        m.invoke(v, this);
                    } catch (Throwable e) {
                    }
                    field.setAccessible(false);

                }

                clz = (Class<? extends Activity>) clz.getSuperclass();

            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    /**
     * 设置状态栏高度
     * @param
     */
    public void setStatusBarHeight(View view) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.height = getStatusBarHeight();
        view.setLayoutParams(params);
    }

    /**
     * 获取状态栏高度
     * @return
     */
    protected int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height",
                "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    /**
     * 设置状态栏颜色，
     * @param color 颜色资源id， 如 R.color.orange</br> 0 黑色
     */
    protected void setStatusBarColor(int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (color == 0) {
                statusBar.setBackgroundColor(Color.BLACK);
            } else {
                statusBar.setBackgroundColor(color);
            }

        }
    }

    /**
     * 屏幕左侧右划返回容器 ,
     *
     * @author Young
     */
//    private class ViewContainer extends LinearLayout {
//
//        private int leftMargin;
//        private VelocityTracker tracker;
//        private float startX;
//        private float startY;
//
//        public ViewContainer(Context context) {
//            super(context);
//            leftMargin = DensityUtil.dip2px(35);
//
//        }
//
//        @Override
//        public boolean dispatchTouchEvent(MotionEvent ev) {
//            if (isGestureOpen == false) {
//                return super.dispatchTouchEvent(ev);
//            }
//            switch (ev.getAction()) {
//                case MotionEvent.ACTION_CANCEL:
//                case MotionEvent.ACTION_UP:
//                    // 当满足下面条件时 视为 右划关闭手势
//                    // 起始按压位置x坐标小与leftMargin&& 向右滑动 && 向右滑动距离 > 竖直方向距离
//                    if (startX < leftMargin
//                            && ev.getRawX() > startX
//                            && ev.getRawX() - startX > Math.abs(ev.getRawY()
//                            - startY)) {
//                        // 速度大于2500时关闭activity
//                        tracker.computeCurrentVelocity(1000);
//                        if (tracker.getXVelocity() > 2500) {
//                            finish();
//                        }
//
//                    }
//
//                    tracker.recycle();
//                    break;
//
//                case MotionEvent.ACTION_DOWN:
//                    startX = ev.getRawX();
//                    startY = ev.getRawY();
//                    tracker = VelocityTracker.obtain();
//                    tracker.addMovement(ev);
//                    break;
//                case MotionEvent.ACTION_MOVE:
//                    tracker.addMovement(ev);
//                    break;
//            }
//
//            return super.dispatchTouchEvent(ev);
//        }
//
//        @Override
//        public boolean onTouchEvent(MotionEvent event) {
//            if (isGestureOpen == false) {
//                return super.onTouchEvent(event);
//            }
//            return true;
//        }
//    }
}
